// 请求接口
function requestApi(url, method = 'GET', data) {
  const app = getApp()
  return new Promise((reslove, reject) => {
    uni.showLoading({
      title: '加载中'
    })
    let header = { }
    if (method === 'POST' || method === 'PUT') {
      header = {
        'content-type': 'application/x-www-form-urlencoded'
      }
    }
    const token = uni.getStorageSync('token')
    if (token) {
      data.token = token
    }
    uni.request({
      url: app.globalData.apiUrl + url,
      data: data,
      method: method,
      header: header,
      success: res => {
        uni.hideLoading()
        if (res.data.code !== 0) {
          uni.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
        reslove(res.data, res)
      },
      fail: msg => {
        uni.hideLoading()
        reject('请求失败')
      }
    })
  })
}

module.exports = {
  requestApi: requestApi
}
